"""v0.2.0

Revision ID: 40c56bc1ba7d
Revises: 6ab71d7090de
Create Date: 2018-08-19 04:39:07.738051

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import Text, Integer, bindparam
from sqlalchemy.sql import table, column, select
import pathlib

# revision identifiers, used by Alembic.
revision = '40c56bc1ba7d'
down_revision = '6ab71d7090de'
branch_labels = None
depends_on = None


def upgrade(engine_name):
    globals()["upgrade_%s" % engine_name]()


def downgrade(engine_name):
    globals()["downgrade_%s" % engine_name]()


page = table("page",
    column('id', Integer),
    column('path', Text),
    )


def upgrade_main():
    conn = op.get_bind()
    if 'sqlite' in conn.dialect.name.lower():
        op.execute("PRAGMA foreign_keys=OFF;")

    pages = conn.execute(select([page.c.id, page.c.path]))
    new_page_paths = []
    for page_id, page_path in pages:
        new_page_paths.append({'page_id': page_id, 'new_path':str(pathlib.Path(page_path).resolve(strict=False))})
    if new_page_paths:
        stmt = page.update().\
                    where(page.c.id==bindparam('page_id')).\
                    values(path=bindparam('new_path'))
        conn.execute(stmt, new_page_paths)

    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('artist', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_artist_user_id'), ['user_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_artist_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('artist_circles', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_artist_circles_artist_id'), ['artist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_artist_circles_circle_id'), ['circle_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_artist_circles_circle_id'), ['circle_id', 'artist_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_artist_circles_artist_id_artist'), 'artist', ['artist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_artist_circles_circle_id_circle'), 'circle', ['circle_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('artist_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_artist_metalists_artist_id'), ['artist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_artist_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_artist_metalists_metalist_id'), ['metalist_id', 'artist_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_artist_metalists_artist_id_artist'), 'artist', ['artist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_artist_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('artist_metatags', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_artist_metatags_artist_id'), ['artist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_artist_metatags_metatag_id'), ['metatag_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_artist_metatags_metatag_id'), ['metatag_id', 'artist_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_artist_metatags_metatag_id_metatag'), 'metatag', ['metatag_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_artist_metatags_artist_id_artist'), 'artist', ['artist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('artist_names', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_artist_names_artist_id'), ['artist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_artist_names_artistname_id'), ['artistname_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_artist_names_artistname_id'), ['artistname_id', 'artist_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_artist_names_artist_id_artist'), 'artist', ['artist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_artist_names_artistname_id_artistname'), 'artistname', ['artistname_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('artist_profiles', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_artist_profiles_artist_id'), ['artist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_artist_profiles_profile_id'), ['profile_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_artist_profiles_profile_id'), ['profile_id', 'artist_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_artist_profiles_artist_id_artist'), 'artist', ['artist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_artist_profiles_profile_id_profile'), 'profile', ['profile_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('artist_url', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_artist_url_artist_id'), ['artist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_artist_url_url_id'), ['url_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_artist_url_url_id'), ['url_id', 'artist_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_artist_url_url_id_url'), 'url', ['url_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_artist_url_artist_id_artist'), 'artist', ['artist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('artistname', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_artistname_alias_for_id'), ['alias_for_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_artistname_language_id'), ['language_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_artistname_name'), ['name'], unique=True)
        
        
        batch_op.create_foreign_key(batch_op.f('fk_artistname_alias_for_id_artistname'), 'artistname', ['alias_for_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_artistname_language_id_language'), 'language', ['language_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('artistname_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_artistname_metalists_artistname_id'), ['artistname_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_artistname_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_artistname_metalists_metalist_id'), ['metalist_id', 'artistname_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_artistname_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_artistname_metalists_artistname_id_artistname'), 'artistname', ['artistname_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('category', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_category_name'), ['name'], unique=True)
        batch_op.create_index(batch_op.f('ix_category_user_id'), ['user_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_category_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('category_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_category_metalists_category_id'), ['category_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_category_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_category_metalists_metalist_id'), ['metalist_id', 'category_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_category_metalists_category_id_category'), 'category', ['category_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_category_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('circle', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_circle_name'), ['name'], unique=True)
        batch_op.create_index(batch_op.f('ix_circle_user_id'), ['user_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_circle_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('circle_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_circle_metalists_circle_id'), ['circle_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_circle_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_circle_metalists_metalist_id'), ['metalist_id', 'circle_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_circle_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_circle_metalists_circle_id_circle'), 'circle', ['circle_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('collection', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_collection_category_id'), ['category_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_collection_name'), ['name'], unique=True)
        batch_op.create_index(batch_op.f('ix_collection_user_id'), ['user_id'], unique=False)
        
        
        batch_op.create_foreign_key(batch_op.f('fk_collection_category_id_category'), 'category', ['category_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_collection_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('collection_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_collection_metalists_collection_id'), ['collection_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_collection_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_collection_metalists_metalist_id'), ['metalist_id', 'collection_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_collection_metalists_collection_id_collection'), 'collection', ['collection_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_collection_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('collection_metatags', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_collection_metatags_collection_id'), ['collection_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_collection_metatags_metatag_id'), ['metatag_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_collection_metatags_metatag_id'), ['metatag_id', 'collection_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_collection_metatags_collection_id_collection'), 'collection', ['collection_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_collection_metatags_metatag_id_metatag'), 'metatag', ['metatag_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('collection_profiles', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_collection_profiles_collection_id'), ['collection_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_collection_profiles_profile_id'), ['profile_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_collection_profiles_profile_id'), ['profile_id', 'collection_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_collection_profiles_profile_id_profile'), 'profile', ['profile_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_collection_profiles_collection_id_collection'), 'collection', ['collection_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('event', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_event_user_id'), ['user_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_event_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('event_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_event_metalists_event_id'), ['event_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_event_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_event_metalists_metalist_id'), ['metalist_id', 'event_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_event_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_event_metalists_event_id_event'), 'event', ['event_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('filter', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_filter_name'), ['name'], unique=True)
        batch_op.create_index(batch_op.f('ix_filter_user_id'), ['user_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_filter_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('filter_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_filter_metalists_filter_id'), ['filter_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_filter_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_filter_metalists_metalist_id'), ['metalist_id', 'filter_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_filter_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_filter_metalists_filter_id_filter'), 'filter', ['filter_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('gallery', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_gallery_category_id'), ['category_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_grouping_id'), ['grouping_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_language_id'), ['language_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_last_read'), ['last_read'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_number'), ['number'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_pub_date'), ['pub_date'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_rating'), ['rating'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_taggable_id'), ['taggable_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_times_read'), ['times_read'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_timestamp'), ['timestamp'], unique=False)
        
        
        
        
        batch_op.create_foreign_key(batch_op.f('fk_gallery_language_id_language'), 'language', ['language_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_gallery_category_id_category'), 'category', ['category_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_gallery_taggable_id_taggable'), 'taggable', ['taggable_id'], ['id'], ondelete='RESTRICT')
        batch_op.create_foreign_key(batch_op.f('fk_gallery_grouping_id_grouping'), 'grouping', ['grouping_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('gallery_artists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_gallery_artists_artist_id'), ['artist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_artists_gallery_id'), ['gallery_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_gallery_artists_artist_id'), ['artist_id', 'gallery_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_gallery_artists_artist_id_artist'), 'artist', ['artist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_gallery_artists_gallery_id_gallery'), 'gallery', ['gallery_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('gallery_collections', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_gallery_collections_collection_id'), ['collection_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_collections_gallery_id'), ['gallery_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_gallery_collections_collection_id'), ['collection_id', 'gallery_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_gallery_collections_gallery_id_gallery'), 'gallery', ['gallery_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_gallery_collections_collection_id_collection'), 'collection', ['collection_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('gallery_filters', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_gallery_filters_filter_id'), ['filter_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_filters_gallery_id'), ['gallery_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_gallery_filters_filter_id'), ['filter_id', 'gallery_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_gallery_filters_gallery_id_gallery'), 'gallery', ['gallery_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_gallery_filters_filter_id_filter'), 'filter', ['filter_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('gallery_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_gallery_metalists_gallery_id'), ['gallery_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_gallery_metalists_metalist_id'), ['metalist_id', 'gallery_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_gallery_metalists_gallery_id_gallery'), 'gallery', ['gallery_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_gallery_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('gallery_metatags', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_gallery_metatags_gallery_id'), ['gallery_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_metatags_metatag_id'), ['metatag_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_gallery_metatags_metatag_id'), ['metatag_id', 'gallery_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_gallery_metatags_gallery_id_gallery'), 'gallery', ['gallery_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_gallery_metatags_metatag_id_metatag'), 'metatag', ['metatag_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('gallery_parodies', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_gallery_parodies_gallery_id'), ['gallery_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_parodies_parody_id'), ['parody_id'], unique=False)
        
        
        batch_op.create_foreign_key(batch_op.f('fk_gallery_parodies_gallery_id_gallery'), 'gallery', ['gallery_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_gallery_parodies_parody_id_parody'), 'parody', ['parody_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('gallery_profiles', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_gallery_profiles_gallery_id'), ['gallery_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_profiles_profile_id'), ['profile_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_gallery_profiles_profile_id'), ['profile_id', 'gallery_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_gallery_profiles_gallery_id_gallery'), 'gallery', ['gallery_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_gallery_profiles_profile_id_profile'), 'profile', ['profile_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('gallery_url', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_gallery_url_gallery_id'), ['gallery_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_gallery_url_url_id'), ['url_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_gallery_url_url_id'), ['url_id', 'gallery_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_gallery_url_url_id_url'), 'url', ['url_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_gallery_url_gallery_id_gallery'), 'gallery', ['gallery_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('grouping', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_grouping_name'), ['name'], unique=True)
        batch_op.create_index(batch_op.f('ix_grouping_status_id'), ['status_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_grouping_status_id_status'), 'status', ['status_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('grouping_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_grouping_metalists_grouping_id'), ['grouping_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_grouping_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_grouping_metalists_metalist_id'), ['metalist_id', 'grouping_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_grouping_metalists_grouping_id_grouping'), 'grouping', ['grouping_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_grouping_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('grouping_profiles', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_grouping_profiles_grouping_id'), ['grouping_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_grouping_profiles_profile_id'), ['profile_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_grouping_profiles_profile_id'), ['profile_id', 'grouping_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_grouping_profiles_profile_id_profile'), 'profile', ['profile_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_grouping_profiles_grouping_id_grouping'), 'grouping', ['grouping_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('hash', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_hash_name'), ['name'], unique=True)

    with op.batch_alter_table('hash_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_hash_metalists_hash_id'), ['hash_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_hash_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_hash_metalists_metalist_id'), ['metalist_id', 'hash_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_hash_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_hash_metalists_hash_id_hash'), 'hash', ['hash_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('language', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_language_name'), ['name'], unique=True)
        batch_op.create_index(batch_op.f('ix_language_user_id'), ['user_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_language_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('language_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_language_metalists_language_id'), ['language_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_language_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_language_metalists_metalist_id'), ['metalist_id', 'language_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_language_metalists_language_id_language'), 'language', ['language_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_language_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('life_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_life_metalists_life_id'), ['life_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_life_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_life_metalists_metalist_id'), ['metalist_id', 'life_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_life_metalists_life_id_life'), 'life', ['life_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_life_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('metalist', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_metalist_name'), ['name'], unique=True)
        batch_op.create_index(batch_op.f('ix_metalist_user_id'), ['user_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_metalist_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('metatag', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_metatag_name'), ['name'], unique=True)

    with op.batch_alter_table('metatag_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_metatag_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_metatag_metalists_metatag_id'), ['metatag_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_metatag_metalists_metalist_id'), ['metalist_id', 'metatag_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_metatag_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_metatag_metalists_metatag_id_metatag'), 'metatag', ['metatag_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('namespace', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_namespace_alias_for_id'), ['alias_for_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_namespace_language_id'), ['language_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_namespace_name'), ['name'], unique=True)
        
        
        batch_op.create_foreign_key(batch_op.f('fk_namespace_language_id_language'), 'language', ['language_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_namespace_alias_for_id_namespace'), 'namespace', ['alias_for_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('namespace_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_namespace_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_namespace_metalists_namespace_id'), ['namespace_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_namespace_metalists_metalist_id'), ['metalist_id', 'namespace_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_namespace_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_namespace_metalists_namespace_id_namespace'), 'namespace', ['namespace_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('namespace_tags', schema=None) as batch_op:
        batch_op.create_index('idx_tag_namespace', ['tag_id', 'namespace_id'], unique=True)
        batch_op.create_index(batch_op.f('ix_namespace_tags_alias_for_id'), ['alias_for_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_namespace_tags_language_id'), ['language_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_namespace_tags_namespace_id'), ['namespace_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_namespace_tags_parent_id'), ['parent_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_namespace_tags_tag_id'), ['tag_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_namespace_tags_user_id'), ['user_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_namespace_tags_tag_id'), ['tag_id', 'namespace_id'])
        
        
        
        
        
        
        batch_op.create_foreign_key(batch_op.f('fk_namespace_tags_alias_for_id_namespace_tags'), 'namespace_tags', ['alias_for_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_namespace_tags_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_namespace_tags_language_id_language'), 'language', ['language_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_namespace_tags_namespace_id_namespace'), 'namespace', ['namespace_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_namespace_tags_tag_id_tag'), 'tag', ['tag_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_namespace_tags_parent_id_namespace_tags'), 'namespace_tags', ['parent_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('namespace_tags_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_namespace_tags_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_namespace_tags_metalists_namespace_tags_id'), ['namespace_tags_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_namespace_tags_metalists_metalist_id'), ['metalist_id', 'namespace_tags_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_namespace_tags_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_namespace_tags_metalists_namespace_tags_id_namespace_tags'), 'namespace_tags', ['namespace_tags_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('namespace_tags_metatags', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_namespace_tags_metatags_metatag_id'), ['metatag_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_namespace_tags_metatags_namespace_tags_id'), ['namespace_tags_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_namespace_tags_metatags_metatag_id'), ['metatag_id', 'namespace_tags_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_namespace_tags_metatags_namespace_tags_id_namespace_tags'), 'namespace_tags', ['namespace_tags_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_namespace_tags_metatags_metatag_id_metatag'), 'metatag', ['metatag_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('page', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_page_gallery_id'), ['gallery_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_page_hash_id'), ['hash_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_page_number'), ['number'], unique=False)
        batch_op.create_index(batch_op.f('ix_page_taggable_id'), ['taggable_id'], unique=False)
        
        
        
        batch_op.create_foreign_key(batch_op.f('fk_page_taggable_id_taggable'), 'taggable', ['taggable_id'], ['id'], ondelete='RESTRICT')
        batch_op.create_foreign_key(batch_op.f('fk_page_gallery_id_gallery'), 'gallery', ['gallery_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_page_hash_id_hash'), 'hash', ['hash_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('page_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_page_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_page_metalists_page_id'), ['page_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_page_metalists_metalist_id'), ['metalist_id', 'page_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_page_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_page_metalists_page_id_page'), 'page', ['page_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('page_metatags', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_page_metatags_metatag_id'), ['metatag_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_page_metatags_page_id'), ['page_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_page_metatags_metatag_id'), ['metatag_id', 'page_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_page_metatags_metatag_id_metatag'), 'metatag', ['metatag_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_page_metatags_page_id_page'), 'page', ['page_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('page_profiles', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_page_profiles_page_id'), ['page_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_page_profiles_profile_id'), ['profile_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_page_profiles_profile_id'), ['profile_id', 'page_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_page_profiles_page_id_page'), 'page', ['page_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_page_profiles_profile_id_profile'), 'profile', ['profile_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('parody', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_parody_user_id'), ['user_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_parody_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('parody_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_parody_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_parody_metalists_parody_id'), ['parody_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_parody_metalists_metalist_id'), ['metalist_id', 'parody_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_parody_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_parody_metalists_parody_id_parody'), 'parody', ['parody_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('parody_names', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_parody_names_parody_id'), ['parody_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_parody_names_parodyname_id'), ['parodyname_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_parody_names_parodyname_id'), ['parodyname_id', 'parody_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_parody_names_parody_id_parody'), 'parody', ['parody_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_parody_names_parodyname_id_parodyname'), 'parodyname', ['parodyname_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('parody_profiles', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_parody_profiles_parody_id'), ['parody_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_parody_profiles_profile_id'), ['profile_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_parody_profiles_profile_id'), ['profile_id', 'parody_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_parody_profiles_parody_id_parody'), 'parody', ['parody_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_parody_profiles_profile_id_profile'), 'profile', ['profile_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('parodyname', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_parodyname_alias_for_id'), ['alias_for_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_parodyname_language_id'), ['language_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_parodyname_name'), ['name'], unique=True)
        
        
        batch_op.create_foreign_key(batch_op.f('fk_parodyname_language_id_language'), 'language', ['language_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_parodyname_alias_for_id_parodyname'), 'parodyname', ['alias_for_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('parodyname_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_parodyname_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_parodyname_metalists_parodyname_id'), ['parodyname_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_parodyname_metalists_metalist_id'), ['metalist_id', 'parodyname_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_parodyname_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_parodyname_metalists_parodyname_id_parodyname'), 'parodyname', ['parodyname_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('profile', schema=None) as batch_op:
        batch_op.create_index('idx_data_size', ['data', 'size'], unique=False)

    with op.batch_alter_table('profile_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_profile_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_profile_metalists_profile_id'), ['profile_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_profile_metalists_metalist_id'), ['metalist_id', 'profile_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_profile_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_profile_metalists_profile_id_profile'), 'profile', ['profile_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('status', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_status_name'), ['name'], unique=True)
        batch_op.create_index(batch_op.f('ix_status_user_id'), ['user_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_status_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('status_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_status_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_status_metalists_status_id'), ['status_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_status_metalists_metalist_id'), ['metalist_id', 'status_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_status_metalists_status_id_status'), 'status', ['status_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_status_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('tag', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_tag_alias_for_id'), ['alias_for_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_tag_language_id'), ['language_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_tag_name'), ['name'], unique=True)
        
        
        batch_op.create_foreign_key(batch_op.f('fk_tag_alias_for_id_tag'), 'tag', ['alias_for_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_tag_language_id_language'), 'language', ['language_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('tag_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_tag_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_tag_metalists_tag_id'), ['tag_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_tag_metalists_metalist_id'), ['metalist_id', 'tag_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_tag_metalists_tag_id_tag'), 'tag', ['tag_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_tag_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('taggable', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_taggable_user_id'), ['user_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_taggable_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('taggable_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_taggable_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_taggable_metalists_taggable_id'), ['taggable_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_taggable_metalists_metalist_id'), ['metalist_id', 'taggable_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_taggable_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_taggable_metalists_taggable_id_taggable'), 'taggable', ['taggable_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('taggable_tags', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_taggable_tags_namespace_tag_id'), ['namespace_tag_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_taggable_tags_taggable_id'), ['taggable_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_taggable_tags_namespace_tag_id'), ['namespace_tag_id', 'taggable_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_taggable_tags_taggable_id_taggable'), 'taggable', ['taggable_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_taggable_tags_namespace_tag_id_namespace_tags'), 'namespace_tags', ['namespace_tag_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('title', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_title_alias_for_id'), ['alias_for_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_title_gallery_id'), ['gallery_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_title_language_id'), ['language_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_title_user_id'), ['user_id'], unique=False)
        
        
        
        
        batch_op.create_foreign_key(batch_op.f('fk_title_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_title_language_id_language'), 'language', ['language_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_title_alias_for_id_title'), 'title', ['alias_for_id'], ['id'], ondelete='SET NULL')
        batch_op.create_foreign_key(batch_op.f('fk_title_gallery_id_gallery'), 'gallery', ['gallery_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('title_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_title_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_title_metalists_title_id'), ['title_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_title_metalists_metalist_id'), ['metalist_id', 'title_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_title_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_title_metalists_title_id_title'), 'title', ['title_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('url', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_url_user_id'), ['user_id'], unique=False)
        
        batch_op.create_foreign_key(batch_op.f('fk_url_user_id_user'), 'user', ['user_id'], ['id'], ondelete='SET NULL')

    with op.batch_alter_table('url_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_url_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_url_metalists_url_id'), ['url_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_url_metalists_metalist_id'), ['metalist_id', 'url_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_url_metalists_url_id_url'), 'url', ['url_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_url_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('user_metalists', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_user_metalists_metalist_id'), ['metalist_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_user_metalists_user_id'), ['user_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_user_metalists_metalist_id'), ['metalist_id', 'user_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_user_metalists_user_id_user'), 'user', ['user_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_user_metalists_metalist_id_metalist'), 'metalist', ['metalist_id'], ['id'], ondelete='CASCADE')

    with op.batch_alter_table('user_metatags', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_user_metatags_metatag_id'), ['metatag_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_user_metatags_user_id'), ['user_id'], unique=False)
        batch_op.create_unique_constraint(batch_op.f('uq_user_metatags_metatag_id'), ['metatag_id', 'user_id'])
        
        
        batch_op.create_foreign_key(batch_op.f('fk_user_metatags_user_id_user'), 'user', ['user_id'], ['id'], ondelete='CASCADE')
        batch_op.create_foreign_key(batch_op.f('fk_user_metatags_metatag_id_metatag'), 'metatag', ['metatag_id'], ['id'], ondelete='CASCADE')

    # ### end Alembic commands ###

    if 'sqlite' in conn.dialect.name.lower():
        op.execute("PRAGMA foreign_keys=ON;")

def downgrade_main():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('user_metatags', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_user_metatags_metatag_id_metatag'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_user_metatags_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.create_foreign_key(None, 'metatag', ['metatag_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_user_metatags_metatag_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_user_metatags_user_id'))
        batch_op.drop_index(batch_op.f('ix_user_metatags_metatag_id'))

    with op.batch_alter_table('user_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_user_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_user_metalists_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_user_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_user_metalists_user_id'))
        batch_op.drop_index(batch_op.f('ix_user_metalists_metalist_id'))

    with op.batch_alter_table('url_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_url_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_url_metalists_url_id_url'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'url', ['url_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_url_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_url_metalists_url_id'))
        batch_op.drop_index(batch_op.f('ix_url_metalists_metalist_id'))

    with op.batch_alter_table('url', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_url_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_url_user_id'))

    with op.batch_alter_table('title_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_title_metalists_title_id_title'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_title_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'title', ['title_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_title_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_title_metalists_title_id'))
        batch_op.drop_index(batch_op.f('ix_title_metalists_metalist_id'))

    with op.batch_alter_table('title', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_title_gallery_id_gallery'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_title_alias_for_id_title'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_title_language_id_language'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_title_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'language', ['language_id'], ['id'])
        batch_op.create_foreign_key(None, 'title', ['alias_for_id'], ['id'])
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.create_foreign_key(None, 'gallery', ['gallery_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_title_user_id'))
        batch_op.drop_index(batch_op.f('ix_title_language_id'))
        batch_op.drop_index(batch_op.f('ix_title_gallery_id'))
        batch_op.drop_index(batch_op.f('ix_title_alias_for_id'))

    with op.batch_alter_table('taggable_tags', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_taggable_tags_namespace_tag_id_namespace_tags'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_taggable_tags_taggable_id_taggable'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'taggable', ['taggable_id'], ['id'])
        batch_op.create_foreign_key(None, 'namespace_tags', ['namespace_tag_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_taggable_tags_namespace_tag_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_taggable_tags_taggable_id'))
        batch_op.drop_index(batch_op.f('ix_taggable_tags_namespace_tag_id'))

    with op.batch_alter_table('taggable_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_taggable_metalists_taggable_id_taggable'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_taggable_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'taggable', ['taggable_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_taggable_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_taggable_metalists_taggable_id'))
        batch_op.drop_index(batch_op.f('ix_taggable_metalists_metalist_id'))

    with op.batch_alter_table('taggable', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_taggable_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_taggable_user_id'))

    with op.batch_alter_table('tag_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_tag_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_tag_metalists_tag_id_tag'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'tag', ['tag_id'], ['id'])
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_tag_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_tag_metalists_tag_id'))
        batch_op.drop_index(batch_op.f('ix_tag_metalists_metalist_id'))

    with op.batch_alter_table('tag', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_tag_language_id_language'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_tag_alias_for_id_tag'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'language', ['language_id'], ['id'])
        batch_op.create_foreign_key(None, 'tag', ['alias_for_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_tag_name'))
        batch_op.drop_index(batch_op.f('ix_tag_language_id'))
        batch_op.drop_index(batch_op.f('ix_tag_alias_for_id'))

    with op.batch_alter_table('status_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_status_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_status_metalists_status_id_status'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'status', ['status_id'], ['id'])
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_status_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_status_metalists_status_id'))
        batch_op.drop_index(batch_op.f('ix_status_metalists_metalist_id'))

    with op.batch_alter_table('status', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_status_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_status_user_id'))
        batch_op.drop_index(batch_op.f('ix_status_name'))

    with op.batch_alter_table('profile_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_profile_metalists_profile_id_profile'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_profile_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'profile', ['profile_id'], ['id'])
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_profile_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_profile_metalists_profile_id'))
        batch_op.drop_index(batch_op.f('ix_profile_metalists_metalist_id'))

    with op.batch_alter_table('profile', schema=None) as batch_op:
        batch_op.drop_index('idx_data_size')

    with op.batch_alter_table('parodyname_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_parodyname_metalists_parodyname_id_parodyname'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_parodyname_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'parodyname', ['parodyname_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_parodyname_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_parodyname_metalists_parodyname_id'))
        batch_op.drop_index(batch_op.f('ix_parodyname_metalists_metalist_id'))

    with op.batch_alter_table('parodyname', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_parodyname_alias_for_id_parodyname'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_parodyname_language_id_language'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'parodyname', ['alias_for_id'], ['id'])
        batch_op.create_foreign_key(None, 'language', ['language_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_parodyname_name'))
        batch_op.drop_index(batch_op.f('ix_parodyname_language_id'))
        batch_op.drop_index(batch_op.f('ix_parodyname_alias_for_id'))

    with op.batch_alter_table('parody_profiles', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_parody_profiles_profile_id_profile'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_parody_profiles_parody_id_parody'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'parody', ['parody_id'], ['id'])
        batch_op.create_foreign_key(None, 'profile', ['profile_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_parody_profiles_profile_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_parody_profiles_profile_id'))
        batch_op.drop_index(batch_op.f('ix_parody_profiles_parody_id'))

    with op.batch_alter_table('parody_names', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_parody_names_parodyname_id_parodyname'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_parody_names_parody_id_parody'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'parody', ['parody_id'], ['id'])
        batch_op.create_foreign_key(None, 'parodyname', ['parodyname_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_parody_names_parodyname_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_parody_names_parodyname_id'))
        batch_op.drop_index(batch_op.f('ix_parody_names_parody_id'))

    with op.batch_alter_table('parody_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_parody_metalists_parody_id_parody'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_parody_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'parody', ['parody_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_parody_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_parody_metalists_parody_id'))
        batch_op.drop_index(batch_op.f('ix_parody_metalists_metalist_id'))

    with op.batch_alter_table('parody', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_parody_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_parody_user_id'))

    with op.batch_alter_table('page_profiles', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_page_profiles_profile_id_profile'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_page_profiles_page_id_page'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'profile', ['profile_id'], ['id'])
        batch_op.create_foreign_key(None, 'page', ['page_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_page_profiles_profile_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_page_profiles_profile_id'))
        batch_op.drop_index(batch_op.f('ix_page_profiles_page_id'))

    with op.batch_alter_table('page_metatags', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_page_metatags_page_id_page'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_page_metatags_metatag_id_metatag'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metatag', ['metatag_id'], ['id'])
        batch_op.create_foreign_key(None, 'page', ['page_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_page_metatags_metatag_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_page_metatags_page_id'))
        batch_op.drop_index(batch_op.f('ix_page_metatags_metatag_id'))

    with op.batch_alter_table('page_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_page_metalists_page_id_page'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_page_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'page', ['page_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_page_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_page_metalists_page_id'))
        batch_op.drop_index(batch_op.f('ix_page_metalists_metalist_id'))

    with op.batch_alter_table('page', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_page_hash_id_hash'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_page_gallery_id_gallery'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_page_taggable_id_taggable'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'hash', ['hash_id'], ['id'])
        batch_op.create_foreign_key(None, 'gallery', ['gallery_id'], ['id'])
        batch_op.create_foreign_key(None, 'taggable', ['taggable_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_page_taggable_id'))
        batch_op.drop_index(batch_op.f('ix_page_number'))
        batch_op.drop_index(batch_op.f('ix_page_hash_id'))
        batch_op.drop_index(batch_op.f('ix_page_gallery_id'))

    with op.batch_alter_table('namespace_tags_metatags', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_namespace_tags_metatags_metatag_id_metatag'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_namespace_tags_metatags_namespace_tags_id_namespace_tags'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'namespace_tags', ['namespace_tags_id'], ['id'])
        batch_op.create_foreign_key(None, 'metatag', ['metatag_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_namespace_tags_metatags_metatag_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_namespace_tags_metatags_namespace_tags_id'))
        batch_op.drop_index(batch_op.f('ix_namespace_tags_metatags_metatag_id'))

    with op.batch_alter_table('namespace_tags_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_namespace_tags_metalists_namespace_tags_id_namespace_tags'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_namespace_tags_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'namespace_tags', ['namespace_tags_id'], ['id'])
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_namespace_tags_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_namespace_tags_metalists_namespace_tags_id'))
        batch_op.drop_index(batch_op.f('ix_namespace_tags_metalists_metalist_id'))

    with op.batch_alter_table('namespace_tags', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_namespace_tags_parent_id_namespace_tags'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_namespace_tags_tag_id_tag'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_namespace_tags_namespace_id_namespace'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_namespace_tags_language_id_language'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_namespace_tags_user_id_user'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_namespace_tags_alias_for_id_namespace_tags'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'namespace_tags', ['alias_for_id'], ['id'])
        batch_op.create_foreign_key(None, 'language', ['language_id'], ['id'])
        batch_op.create_foreign_key(None, 'tag', ['tag_id'], ['id'])
        batch_op.create_foreign_key(None, 'namespace_tags', ['parent_id'], ['id'])
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.create_foreign_key(None, 'namespace', ['namespace_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_namespace_tags_tag_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_namespace_tags_user_id'))
        batch_op.drop_index(batch_op.f('ix_namespace_tags_tag_id'))
        batch_op.drop_index(batch_op.f('ix_namespace_tags_parent_id'))
        batch_op.drop_index(batch_op.f('ix_namespace_tags_namespace_id'))
        batch_op.drop_index(batch_op.f('ix_namespace_tags_language_id'))
        batch_op.drop_index(batch_op.f('ix_namespace_tags_alias_for_id'))
        batch_op.drop_index('idx_tag_namespace')

    with op.batch_alter_table('namespace_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_namespace_metalists_namespace_id_namespace'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_namespace_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'namespace', ['namespace_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_namespace_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_namespace_metalists_namespace_id'))
        batch_op.drop_index(batch_op.f('ix_namespace_metalists_metalist_id'))

    with op.batch_alter_table('namespace', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_namespace_alias_for_id_namespace'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_namespace_language_id_language'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'namespace', ['alias_for_id'], ['id'])
        batch_op.create_foreign_key(None, 'language', ['language_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_namespace_name'))
        batch_op.drop_index(batch_op.f('ix_namespace_language_id'))
        batch_op.drop_index(batch_op.f('ix_namespace_alias_for_id'))

    with op.batch_alter_table('metatag_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_metatag_metalists_metatag_id_metatag'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_metatag_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'metatag', ['metatag_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_metatag_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_metatag_metalists_metatag_id'))
        batch_op.drop_index(batch_op.f('ix_metatag_metalists_metalist_id'))

    with op.batch_alter_table('metatag', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_metatag_name'))

    with op.batch_alter_table('metalist', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_metalist_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_metalist_user_id'))
        batch_op.drop_index(batch_op.f('ix_metalist_name'))

    with op.batch_alter_table('life_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_life_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_life_metalists_life_id_life'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'life', ['life_id'], ['id'])
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_life_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_life_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_life_metalists_life_id'))

    with op.batch_alter_table('language_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_language_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_language_metalists_language_id_language'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'language', ['language_id'], ['id'])
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_language_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_language_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_language_metalists_language_id'))

    with op.batch_alter_table('language', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_language_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_language_user_id'))
        batch_op.drop_index(batch_op.f('ix_language_name'))

    with op.batch_alter_table('hash_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_hash_metalists_hash_id_hash'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_hash_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'hash', ['hash_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_hash_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_hash_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_hash_metalists_hash_id'))

    with op.batch_alter_table('hash', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_hash_name'))

    with op.batch_alter_table('grouping_profiles', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_grouping_profiles_grouping_id_grouping'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_grouping_profiles_profile_id_profile'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'profile', ['profile_id'], ['id'])
        batch_op.create_foreign_key(None, 'grouping', ['grouping_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_grouping_profiles_profile_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_grouping_profiles_profile_id'))
        batch_op.drop_index(batch_op.f('ix_grouping_profiles_grouping_id'))

    with op.batch_alter_table('grouping_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_grouping_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_grouping_metalists_grouping_id_grouping'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'grouping', ['grouping_id'], ['id'])
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_grouping_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_grouping_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_grouping_metalists_grouping_id'))

    with op.batch_alter_table('grouping', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_grouping_status_id_status'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'status', ['status_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_grouping_status_id'))
        batch_op.drop_index(batch_op.f('ix_grouping_name'))

    with op.batch_alter_table('gallery_url', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_gallery_url_gallery_id_gallery'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_gallery_url_url_id_url'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'gallery', ['gallery_id'], ['id'])
        batch_op.create_foreign_key(None, 'url', ['url_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_gallery_url_url_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_gallery_url_url_id'))
        batch_op.drop_index(batch_op.f('ix_gallery_url_gallery_id'))

    with op.batch_alter_table('gallery_profiles', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_gallery_profiles_profile_id_profile'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_gallery_profiles_gallery_id_gallery'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'profile', ['profile_id'], ['id'])
        batch_op.create_foreign_key(None, 'gallery', ['gallery_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_gallery_profiles_profile_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_gallery_profiles_profile_id'))
        batch_op.drop_index(batch_op.f('ix_gallery_profiles_gallery_id'))

    with op.batch_alter_table('gallery_parodies', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_gallery_parodies_parody_id_parody'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_gallery_parodies_gallery_id_gallery'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'gallery', ['gallery_id'], ['id'])
        batch_op.create_foreign_key(None, 'parody', ['parody_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_gallery_parodies_parody_id'))
        batch_op.drop_index(batch_op.f('ix_gallery_parodies_gallery_id'))

    with op.batch_alter_table('gallery_metatags', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_gallery_metatags_metatag_id_metatag'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_gallery_metatags_gallery_id_gallery'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'gallery', ['gallery_id'], ['id'])
        batch_op.create_foreign_key(None, 'metatag', ['metatag_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_gallery_metatags_metatag_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_gallery_metatags_metatag_id'))
        batch_op.drop_index(batch_op.f('ix_gallery_metatags_gallery_id'))

    with op.batch_alter_table('gallery_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_gallery_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_gallery_metalists_gallery_id_gallery'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'gallery', ['gallery_id'], ['id'])
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_gallery_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_gallery_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_gallery_metalists_gallery_id'))

    with op.batch_alter_table('gallery_filters', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_gallery_filters_filter_id_filter'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_gallery_filters_gallery_id_gallery'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'gallery', ['gallery_id'], ['id'])
        batch_op.create_foreign_key(None, 'filter', ['filter_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_gallery_filters_filter_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_gallery_filters_gallery_id'))
        batch_op.drop_index(batch_op.f('ix_gallery_filters_filter_id'))

    with op.batch_alter_table('gallery_collections', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_gallery_collections_collection_id_collection'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_gallery_collections_gallery_id_gallery'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'gallery', ['gallery_id'], ['id'])
        batch_op.create_foreign_key(None, 'collection', ['collection_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_gallery_collections_collection_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_gallery_collections_gallery_id'))
        batch_op.drop_index(batch_op.f('ix_gallery_collections_collection_id'))

    with op.batch_alter_table('gallery_artists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_gallery_artists_gallery_id_gallery'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_gallery_artists_artist_id_artist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'gallery', ['gallery_id'], ['id'])
        batch_op.create_foreign_key(None, 'artist', ['artist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_gallery_artists_artist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_gallery_artists_gallery_id'))
        batch_op.drop_index(batch_op.f('ix_gallery_artists_artist_id'))

    with op.batch_alter_table('gallery', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_gallery_grouping_id_grouping'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_gallery_taggable_id_taggable'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_gallery_category_id_category'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_gallery_language_id_language'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'grouping', ['grouping_id'], ['id'])
        batch_op.create_foreign_key(None, 'taggable', ['taggable_id'], ['id'])
        batch_op.create_foreign_key(None, 'category', ['category_id'], ['id'])
        batch_op.create_foreign_key(None, 'language', ['language_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_gallery_timestamp'))
        batch_op.drop_index(batch_op.f('ix_gallery_times_read'))
        batch_op.drop_index(batch_op.f('ix_gallery_taggable_id'))
        batch_op.drop_index(batch_op.f('ix_gallery_rating'))
        batch_op.drop_index(batch_op.f('ix_gallery_pub_date'))
        batch_op.drop_index(batch_op.f('ix_gallery_number'))
        batch_op.drop_index(batch_op.f('ix_gallery_last_read'))
        batch_op.drop_index(batch_op.f('ix_gallery_language_id'))
        batch_op.drop_index(batch_op.f('ix_gallery_grouping_id'))
        batch_op.drop_index(batch_op.f('ix_gallery_category_id'))

    with op.batch_alter_table('filter_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_filter_metalists_filter_id_filter'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_filter_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'filter', ['filter_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_filter_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_filter_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_filter_metalists_filter_id'))

    with op.batch_alter_table('filter', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_filter_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_filter_user_id'))
        batch_op.drop_index(batch_op.f('ix_filter_name'))

    with op.batch_alter_table('event_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_event_metalists_event_id_event'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_event_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'event', ['event_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_event_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_event_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_event_metalists_event_id'))

    with op.batch_alter_table('event', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_event_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_event_user_id'))

    with op.batch_alter_table('collection_profiles', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_collection_profiles_collection_id_collection'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_collection_profiles_profile_id_profile'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'profile', ['profile_id'], ['id'])
        batch_op.create_foreign_key(None, 'collection', ['collection_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_collection_profiles_profile_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_collection_profiles_profile_id'))
        batch_op.drop_index(batch_op.f('ix_collection_profiles_collection_id'))

    with op.batch_alter_table('collection_metatags', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_collection_metatags_metatag_id_metatag'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_collection_metatags_collection_id_collection'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'collection', ['collection_id'], ['id'])
        batch_op.create_foreign_key(None, 'metatag', ['metatag_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_collection_metatags_metatag_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_collection_metatags_metatag_id'))
        batch_op.drop_index(batch_op.f('ix_collection_metatags_collection_id'))

    with op.batch_alter_table('collection_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_collection_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_collection_metalists_collection_id_collection'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'collection', ['collection_id'], ['id'])
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_collection_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_collection_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_collection_metalists_collection_id'))

    with op.batch_alter_table('collection', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_collection_user_id_user'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_collection_category_id_category'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'category', ['category_id'], ['id'])
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_collection_user_id'))
        batch_op.drop_index(batch_op.f('ix_collection_name'))
        batch_op.drop_index(batch_op.f('ix_collection_category_id'))

    with op.batch_alter_table('circle_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_circle_metalists_circle_id_circle'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_circle_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'circle', ['circle_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_circle_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_circle_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_circle_metalists_circle_id'))

    with op.batch_alter_table('circle', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_circle_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_circle_user_id'))
        batch_op.drop_index(batch_op.f('ix_circle_name'))

    with op.batch_alter_table('category_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_category_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_category_metalists_category_id_category'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'category', ['category_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_category_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_category_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_category_metalists_category_id'))

    with op.batch_alter_table('category', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_category_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_category_user_id'))
        batch_op.drop_index(batch_op.f('ix_category_name'))

    with op.batch_alter_table('artistname_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_artistname_metalists_artistname_id_artistname'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_artistname_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'artistname', ['artistname_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_artistname_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_artistname_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_artistname_metalists_artistname_id'))

    with op.batch_alter_table('artistname', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_artistname_language_id_language'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_artistname_alias_for_id_artistname'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'language', ['language_id'], ['id'])
        batch_op.create_foreign_key(None, 'artistname', ['alias_for_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_artistname_name'))
        batch_op.drop_index(batch_op.f('ix_artistname_language_id'))
        batch_op.drop_index(batch_op.f('ix_artistname_alias_for_id'))

    with op.batch_alter_table('artist_url', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_artist_url_artist_id_artist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_artist_url_url_id_url'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'url', ['url_id'], ['id'])
        batch_op.create_foreign_key(None, 'artist', ['artist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_artist_url_url_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_artist_url_url_id'))
        batch_op.drop_index(batch_op.f('ix_artist_url_artist_id'))

    with op.batch_alter_table('artist_profiles', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_artist_profiles_profile_id_profile'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_artist_profiles_artist_id_artist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'profile', ['profile_id'], ['id'])
        batch_op.create_foreign_key(None, 'artist', ['artist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_artist_profiles_profile_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_artist_profiles_profile_id'))
        batch_op.drop_index(batch_op.f('ix_artist_profiles_artist_id'))

    with op.batch_alter_table('artist_names', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_artist_names_artistname_id_artistname'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_artist_names_artist_id_artist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'artist', ['artist_id'], ['id'])
        batch_op.create_foreign_key(None, 'artistname', ['artistname_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_artist_names_artistname_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_artist_names_artistname_id'))
        batch_op.drop_index(batch_op.f('ix_artist_names_artist_id'))

    with op.batch_alter_table('artist_metatags', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_artist_metatags_artist_id_artist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_artist_metatags_metatag_id_metatag'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metatag', ['metatag_id'], ['id'])
        batch_op.create_foreign_key(None, 'artist', ['artist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_artist_metatags_metatag_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_artist_metatags_metatag_id'))
        batch_op.drop_index(batch_op.f('ix_artist_metatags_artist_id'))

    with op.batch_alter_table('artist_metalists', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_artist_metalists_metalist_id_metalist'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_artist_metalists_artist_id_artist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'metalist', ['metalist_id'], ['id'])
        batch_op.create_foreign_key(None, 'artist', ['artist_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_artist_metalists_metalist_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_artist_metalists_metalist_id'))
        batch_op.drop_index(batch_op.f('ix_artist_metalists_artist_id'))

    with op.batch_alter_table('artist_circles', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_artist_circles_circle_id_circle'), type_='foreignkey')
        batch_op.drop_constraint(batch_op.f('fk_artist_circles_artist_id_artist'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'artist', ['artist_id'], ['id'])
        batch_op.create_foreign_key(None, 'circle', ['circle_id'], ['id'])
        batch_op.drop_constraint(batch_op.f('uq_artist_circles_circle_id'), type_='unique')
        batch_op.drop_index(batch_op.f('ix_artist_circles_circle_id'))
        batch_op.drop_index(batch_op.f('ix_artist_circles_artist_id'))

    with op.batch_alter_table('artist', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_artist_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key(None, 'user', ['user_id'], ['id'])
        batch_op.drop_index(batch_op.f('ix_artist_user_id'))

    # ### end Alembic commands ###

